package com.anXin.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.anXin.model.common.dtos.ResponseResult;
import com.anXin.model.common.enums.AppHttpCodeEnum;
import com.anXin.model.user.pojos.User;
import com.anXin.model.user.pojos.UserFan;
import com.anXin.model.user.pojos.UserFollow;
import com.anXin.user.mapper.UserFollowMapper;
import com.anXin.user.service.UserFanService;
import com.anXin.user.service.UserFollowService;
import com.anXin.user.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;


@Service("userFollowService")
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Resource
    @Lazy
    private UserService userService;
    @Resource
    private UserFanService userFanService;
    @Override
    public ResponseResult isFollow(Long userId) {
        UserFollow one = query().eq("user_id", AppThreadLocalUtil.getUser().getId()).eq("follow_id", userId).one();
        if (ObjectUtil.isEmpty(one))return ResponseResult.errorResult(AppHttpCodeEnum.DATA_LIST_NULL);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult followList(Long userId) {
        List<UserFollow> userFollows = query().eq("user_id", userId).list();
        if (userFollows.isEmpty())return ResponseResult.errorResult(AppHttpCodeEnum.DATA_LIST_NULL);
        return ResponseResult.okResult(userFollows);
    }

    @Override
    public ResponseResult follow(Long userId) {
        ResponseResult isFollow = isFollow(userId);
        if (isFollow.getCode()==200){
            UserFollow userFollow = query().eq("user_id", AppThreadLocalUtil.getUser().getId()).eq("follow_id", userId).one();
            boolean remove1 = removeById(userFollow.getId());
            UserFan userFan = userFanService.query().eq("user_id", userId).eq("fans_id", AppThreadLocalUtil.getUser().getId()).one();
            boolean remove2 = userFanService.removeById(userFan.getId());
            if (remove1&remove2)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

        }else {
            User followUser = userService.query().eq("id", userId).one();
            User fansUser = userService.query().eq("id", AppThreadLocalUtil.getUser().getId()).one();
            UserFollow userFollow = new UserFollow();
            userFollow.setFollowId(followUser.getId());
            userFollow.setUserId(fansUser.getId());
            userFollow.setFollowName(followUser.getName());

            boolean save1 = save(userFollow);
            UserFan userFan = new UserFan();
            userFan.setFansId(fansUser.getId());
            userFan.setUserId(followUser.getId());
            userFan.setFansName(fansUser.getName());
            boolean save2 = userFanService.save(userFan);
            if (save1&save2)return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public Integer followCount(Long userId) {
        return query().eq("user_id", userId).count();
    }
}